package fr.isen.smssender.contact;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import fr.isen.smssender.HomeActivity;
import fr.isen.smssender.R;
import fr.isen.smssender.database.DBManager;

import java.util.Date;


public class ContactParameterActivity extends Activity {

    private EditText editName;
    private EditText editFirstName;
    private int idContact;
    private DBManager myDBManager;
    private String name, firstName, number;
    private Contact current;


    private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;


    static final int DATE_DIALOG_ID = 0;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_parameter);
        Bundle bundle = getIntent().getExtras();
        idContact = bundle.getInt("id");


        myDBManager = new DBManager(this);

        current = new Contact(idContact, bundle.getString("name"), bundle.getString("firstName"), new Date(bundle.getString("date")), bundle.getString("number"));
        editName = (EditText) findViewById(R.id.editTextName);
        editFirstName = (EditText) findViewById(R.id.editTextFirstName);
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        editName.setText(current.getNom());
        editFirstName.setText(current.getPrenom());
        Date currentDate = current.getBirthday();
        mYear = currentDate.getYear();
        mDay = currentDate.getDay();
        mMonth = currentDate.getMonth();
        // mDateDisplay.setText(mDay + " - " + mMonth + " - " + mYear);



        // Bouton Return Activity

        Button b3 = (Button) findViewById(R.id.buttonBack);
        b3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                returnMainActivity(v);
            }
        });


        final Button button = (Button) findViewById(R.id.buttonChangeName);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String changeText = editName.getText().toString();
                current.setNom(changeText);
                current.setPrenom(editFirstName.getText().toString());
                current.setBirthday(new Date(mYear, mMonth, mDay));
                myDBManager.setContact(current);
                returnMainActivity(v);
            }
        });


        // capture our View elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);

        // add a click listener to the button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        // get the current date
        /*final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
*/
        // display the current date (this method is below)
        updateDisplay();
    }

    // updates the date in the TextView
    private void updateDisplay() {
        mDateDisplay.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "));

    }

    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        }
        return null;
    }


    public void returnMainActivity(View v) {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }


}
